package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    //分页查询
    @Override
    public PageResult findPage(Integer pageNum, Integer sizeNum, String queryString) {
        PageHelper.startPage(pageNum,sizeNum);
        Page<Menu> page = menuDao.findPage(queryString);
     /*   List<Menu> result = page.getResult();
        result.stream().forEach(menu -> {
            List<Menu> children = menu.getChildren();
            System.out.println(children.size());
        });*/

        return new PageResult(page.getTotal(),page.getResult());
    }

    //查询所有菜单
    @Override
    public List<Menu> findParent() {
        return menuDao.findParent();
    }

    //新增菜单
    @Override
    public void addMenu(Menu menu) {

        if (menu.getParentMenuId()==null){
            menu.setLevel(1);
        }else {
            menu.setLevel(2);
        }

        menuDao.add(menu);
    }
    //查询套餐数据
    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    //编辑套餐数据
    @Override
    public void updateMenu(Menu menu) {

        if (menu.getParentMenuId()==null){
            menu.setLevel(1);
        }else {
            menu.setLevel(2);
        }
        menuDao.update(menu);
    }
    //删除表单数据
    @Override
    public void delete(Integer id) {
        menuDao.delete(id);
    }

    //查看子菜单数据
    @Override
    public List<Menu> findChildrens(Integer id) {

        return menuDao.findChildren(id);
    }

    @Override
    public List<Menu> findAll() {
            return menuDao.findAll();
    }
    //查询角色关联的菜单id
    @Override
    public Integer[] findMenuIds(Integer rid) {
        return menuDao.findMenuIds(rid);
    }
}
